<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Common\Api\WXPAY\lib\WxPay;
use Common\Api\SendPhoneCodeApi;
class PayController extends HomeController {
 	
	public function _initialize(){
		
 		parent::_initialize();
	}
	/**
	 * 获取支付金额
	 */
	public function getPayMoney(){
		$open_id = session('openid');
		$order_id = I('order_id');
		$order = M('Orders')->where(array('order_id'=>$order_id))->find();
		if($order['send_money'] == 9999){
		    $order['send_money'] = 0;
		}
		if(!$order){
		    $this->ajaxReturn(['status'=>-1,'msg'=>'不存在此订单'],'JSONP');
		}
		if($open_id!=$order['open_id']){
		    $this->ajaxReturn(['status'=>-2,'msg'=>'此订单不是登陆用户'],'JSONP');
		}
		$member = M('Member')->where(array('openid'=>$open_id))->find();
		$order['goods'] = M('Order_goods')->where(array('order_id'=>$order_id))->select();
		foreach ($order['goods'] as &$v){
		    $all_money += $v['num'] * $v['price']; 
		    $all_jifen += $v['num'] * $v['jifen']; 
		}
		if($all_jifen == 0){
		    if($member['money'] < $all_money+$order['send_money']){
		        $this->ajaxReturn(['status'=>-3,'msg'=>'您的余额不足','jifen'=>$member['jifen'],'money'=>$member['money']],'JSONP');
		    }else{
		        $this->ajaxReturn(['status'=>-4,'msg'=>'积分不可用','jifen'=>$member['jifen'],'money'=>$member['money']],'JSONP');
		    }
		    
		}
            if($all_money == 0){
           	    if($member['jifen'] < $all_jifen+$order['send_money']){
		        $this->ajaxReturn(['status'=>-5,'msg'=>'积分不足','jifen'=>$member['jifen'],'money'=>$member['money']],'JSONP');
		    }else{
		        $this->ajaxReturn(['status'=>-6,'msg'=>'余额不可用','jifen'=>$member['jifen'],'money'=>$member['money']],'JSONP');
		    }
            }
            
            //elseif($member['money'] < $all_money+$order['send_money']){
	//	    if($member['jifen'] < $all_jifen){
	//	        $this->ajaxReturn(['status'=>-4,'msg'=>'您的积分不足，您的余额不足'],'JSONP');
	//	    }else{
	//	        $this->ajaxReturn(['status'=>-7,'msg'=>'您的余额不足'],'JSONP');
	//	    }
	//	  }else{
	//	      if($member['jifen'] < $all_jifen){
	//	          $this->ajaxReturn(['status'=>-6,'msg'=>'您的积分不足'],'JSONP');
	//	      }
		  //}
		  
		  $this->ajaxReturn(['status'=>1,'msg'=>'你可通过任意方式付款','jifen'=>$member['jifen'],'money'=>$member['money']],'JSONP');
		
		
		
	}
	
	/**
	 * 确定支付
	 * 
	 */
	public function toPay(){
		//扣除会员积分
		//支付成功
		//修改订单状态
		$open_id = session('openid');
		$order_id = I('order_id');
		$pay_type = I('pay_type');
		
	    $order_id = I('order_id');
		$order = M('Orders')->where(array('order_id'=>$order_id))->find();
		if(!$order){
		    $this->ajaxReturn(['status'=>-1,'msg'=>'不存在此订单'],'JSONP');
		}
		if($open_id!=$order['open_id']){
		    $this->ajaxReturn(['status'=>-2,'msg'=>'此订单不是登陆用户'],'JSONP');
		}
		if($order['is_pay'] ==1 && $order['order_status'] != -1){
		    $this->ajaxReturn(['status'=>-3,'msg'=>'此订单已付款，请勿重复提交'],'JSONP');
		}
		
		$order['goods'] = M('Order_goods')->where(array('order_id'=>$order_id))->select();
		
		foreach ($order['goods'] as &$v){
		    $all_money += $v['num'] * $v['price']; 
		    $all_jifen += $v['num'] * $v['jifen']; 
		    $all_fanjifen += $v['num'] * $v['fanjifen']; 
		}
		
		$member = M('Member')->where(array('openid'=>$open_id))->find();
		if($order['send_money'] == 9999){
			$order['send_money'] = 0;
		}
		//1微信 2余额 3积分
		if($pay_type == 3 ){
		    if($all_jifen == 0){
		        $this->ajaxReturn(['status'=>-3,'msg'=>'积分不可用'],'JSONP');
		    }
		    if($member['jifen'] < $all_jifen+$order['send_money']){
		        $this->ajaxReturn(['status'=>-4,'msg'=>'您的积分不足'],'JSONP');
		    }
		    M()->startTrans();
		    $re[] = M('Member')->where(array('openid'=>$open_id))->setDec('jifen',$all_jifen+$order['send_money']);
		    //状态1，支付完成，等待发货
		    $re[] = M('Orders')->where(array('order_id'=>$order_id))->save(array('order_status'=>1,'is_pay'=>1,'pay_type'=>3,'pay_time'=>time(),'total_money'=>$all_jifen+$order['send_money']));
		    $re[] = $this->addFinance(2,2,$all_jifen+$order['send_money'],'购买商品');
		    if(in_array(false, $re)){
		        M()->rollback();
		        $this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
		    }else{
		        M()->commit();
				$SENDPHONEMESSAGE = new SendPhoneCodeApi('15507328888');
                          $SENDPHONEMESSAGE->send($order['order_no']); 
		        $this->ajaxReturn(['status'=>1,'msg'=>'支付成功'],'JSONP');
		    }
		}
		if($pay_type == 2){
		    if($member['money'] < $all_money+$order['send_money']){
		        $this->ajaxReturn(['status'=>-5,'msg'=>'您的余额不足'],'JSONP');
		    }
		    M()->startTrans();
		    $re[] = M('Member')->where(array('openid'=>$open_id))->setDec('money',$all_money+$order['send_money']);
		    //状态1，支付完成，等待发货
		    $re[] = M('Orders')->where(array('order_id'=>$order_id))->save(array('order_status'=>1,'is_pay'=>1,'pay_type'=>2,'pay_time'=>time()));
		    $re[] = $this->addFinance(2,1,$all_money+$order['send_money'],'购买商品');
		    $re[] = M('Member')->where(array('openid'=>$open_id))->setInc('jifen',$all_fanjifen);
		    $re[] = $this->addFinance(1,2,$all_fanjifen,'购买商品返还积分');
		    if(in_array(false, $re)){
		        M()->rollback();
		        $this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
		    }else{
		        M()->commit();
                            $SENDPHONEMESSAGE = new SendPhoneCodeApi('15507328888');
                          $SENDPHONEMESSAGE->send($order['order_no']); 
		        $this->ajaxReturn(['status'=>1,'msg'=>'支付成功'],'JSONP');
		    }
		}
		//是否需要事务
		if($pay_type == 1){
			Vendor('WxPays.example.jsapi','','.php');
	//$open_id = 'oiWK9v2R6E24rEOM4lVcFipwXE1g';
			$a = new \JSAPI();
			//$wocao = ['status'=>1];
			$wocao = $a->getCanshu($open_id,$all_money+$order['send_money'],"购买商品");
			//dump($wocao);die;
			$this->ajaxReturn($wocao,'JSONP');
			}
                    if($pay_type == 4){
		    //状态1，支付完成，等待发货
		    $re[] = M('Orders')->where(array('order_id'=>$order_id))->save(array('order_status'=>1,'is_pay'=>1,'pay_type'=>1,'pay_time'=>time()));
		    $re[] = $this->addFinance(2,1,$all_money+$order['send_money'],'购买商品');
		    $re[] = M('Member')->where(array('openid'=>$open_id))->setInc('jifen',$all_fanjifen);
		    $re[] = $this->addFinance(1,2,$all_fanjifen,'购买商品返还积分');
		    if(in_array(false, $re)){
		        $this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
		    }else{
				 $SENDPHONEMESSAGE = new SendPhoneCodeApi('15507328888');
                          $SENDPHONEMESSAGE->send($order['order_no']);
		        $this->ajaxReturn(['status'=>1,'msg'=>'支付成功'],'JSONP');
		    }
                    }
}
  public function ceshi(){
  	Vendor('WxPays.example.jsapi','','.php');
	$open_id = session('openid');
	$buyjifen = I('buyjifen');
	if($buyjifen){
	    $money= I('money');
	    $a = new \JSAPI();
	    //$wocao = ['status'=>1];
	    $wocao = $a->getCanshu($open_id,$money,"购买积分");
	    //dump($wocao);die;
	    $this->ajaxReturn($wocao,'JSONP');
	}else{
	    $money= I('money');
	    $a = new \JSAPI();
	    //$wocao = ['status'=>1];
	    $wocao = $a->getCanshu($open_id,$money,"微信充值");
	    //dump($wocao);die;
	    $this->ajaxReturn($wocao,'JSONP');
	}
	
  }

  public function recharge(){
      $buyjifen = I('buyjifen');
  	$money = I('money');
  	if($buyjifen){
  	    $bili = M("Config")->where(array('key'=>'JIFENBILI'))->find();
  	    $jifen = $money * $bili['value'];
  	    $open_id = session('openid');
  	    $re[] = M('Member')->where(array('openid'=>$open_id))->setInc('jifen',$jifen);
  	    $re[] = $this->addFinance(1,2,$money,'购买积分');
  	    if(in_array(false,$re)){
  	        $this->ajaxReturn(['status'=>-1,'msg'=>'支付失败'],'JSONP');
  	    }else{
  	        $this->ajaxReturn(['status'=>1,'msg'=>'支付成功'],'JSONP');
  	    }
  	    
  	}else{
  	    $open_id = session('openid');
  	    $re[] = M('Member')->where(array('openid'=>$open_id))->setInc('money',$money);
  	    $re[] = $this->addFinance(1,1,$money,'微信充值');
  	    if(in_array(false,$re)){
  	        $this->ajaxReturn(['status'=>-1,'msg'=>'支付失败'],'JSONP');
  	    }else{
  	        $this->ajaxReturn(['status'=>1,'msg'=>'支付成功'],'JSONP');
  	    }
  	}
    	
  }
}